home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Containrs / sa / multi_incl < prev    next >
Text File  |  1996-06-01  |  5KB  |  161 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- multimap_incl.sa: Multimap include partial class
  3. -- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
  4. -- Copyright (C) 1995, International Computer Science Institute
  5. -- $Id: multimap_incl.sa,v 1.6 1996/06/01 21:36:30 gomes Exp $
  6. --
  7. -- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
  8. -- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
  9. -- LICENSE contained in the file: Sather/Doc/License of the
  10. -- Sather distribution. The license is also available from ICSI,
  11. -- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
  12. -------------------------------------------------------------------
  13. class MULTIMAP{K,E} is
  14.    include H_MULTIMAP{K,E};
  15. end;
  16. -------------------------------------------------------------------
  17. partial class RO_MULTIMAP_INCL{K,E} is
  18.    -- Partial class for multi-maps
  19.    include COMPARE{E};
  20.    
  21.    --           --------- CORE FEATURES (must define) ---------
  22.    stub size: INT;
  23.    -- The number of elements in this multimap
  24.    
  25.    stub n_inds: INT;
  26.    -- Return the total number of indices
  27.  
  28.    stub elt!: E;
  29.    -- Yield elements (unordered)
  30.    
  31.    stub ind!: K;
  32.    -- Yield element indices (unordered)
  33.    
  34.    stub pair!: TUP{K,E}; 
  35.    -- Yield pairs of index,element
  36.    
  37.    stub target!(once k:K): E; 
  38.    -- Yield the targets of the index "k"
  39.    
  40.    stub n_targets(k:K): INT; 
  41.    -- Return the number of targets for index "k"
  42.    
  43.    --              ------ Access/Measurement --------------
  44.    targets(k: K): BAG{E} is
  45.       res ::= #BAG{E};
  46.       loop res.insert(target!(k)) end;
  47.       return res;
  48.    end;
  49.  
  50.    inds: ARRAY{K} is
  51.       -- Return an index array which is the same size as self and
  52.       -- is set to the values of the indices
  53.       sz: INT := n_inds;
  54.       res: ARRAY{K} := #(sz);
  55.       i: INT := 0;
  56.       loop until!(i >= sz); res[i] := ind!; i := i + 1; end;
  57.       return res;
  58.    end;
  59.    
  60.    target!: E is loop yield elt! end; end;
  61.  
  62.    --              ------ Queries/Comparison --------------
  63.    has_ind(k: K): BOOL is return n_targets(k) > 0 end;
  64.    -- Return true if the index "k" has at least one target in this
  65.    -- multimap
  66.  
  67.    has_elt(e: E): BOOL is return has(e) end;
  68.    
  69.    has(e: E): BOOL is
  70.       -- Return true if this multimap has the element "e"
  71.       loop t ::= elt!; if elt_eq(t,e) then return true end; end;
  72.       return false;
  73.    end;
  74.       
  75.    equals(b: $RO_MULTIMAP{K,E}): BOOL is
  76.       -- Returns true if all of "e"'s elements are equal to self's elts
  77.       -- Ordering is an issue. Should be redefined to be more
  78.       -- precise for particular descendants
  79.       if n_inds /= b.n_inds then return false end;
  80.       if size /= b.size then return false end;
  81.       loop e ::= ind!; 
  82.      if n_targets(e) /= b.n_targets(e) then return false end; 
  83.       end;
  84.       return true ;
  85.    end;
  86.  
  87.    elt_if(test:ROUT{E}:BOOL,out res:E):BOOL is
  88.       -- Return the first element that satisfies "test" in "res"
  89.       -- Return true if a element was found, false otherwise
  90.       loop r ::= elt!; if test.call(r) then res := r; return true; end;  end; 
  91.       return false;
  92.    end;
  93.  
  94.    ind_if(test:ROUT{E}:BOOL):K is
  95.       -- Return the index of the leftmost element that satisfies `test', 
  96.       -- or void if there is none. 
  97.       -- Must be changed to use an out argument
  98.       loop 
  99.      r::=ind!;
  100.      loop e ::= target!(r);
  101.         if test.call(e) then return r end 
  102.      end;
  103.       end; 
  104.       return void 
  105.    end;
  106.  
  107.    --              ------ Cursor --------------------------
  108.    filter!(once f:ROUT{E}:BOOL): E  pre ~void(self) is
  109.       -- Yield all elements that satisfy the boolean predicate "f"
  110.       loop
  111.      e ::= target!;
  112.      if f.call(e) then yield e end
  113.       end
  114.    end;
  115.  
  116.    filter_not!(once f:ROUT{E}:BOOL): E  pre ~void(self) is
  117.       -- Yield all elements that do not satisfy the boolean predicate "f"
  118.       loop e ::= target!; if ~f.call(e) then yield e end  end
  119.    end;
  120.    
  121.    --              ------ Conversion ----------------------
  122.     str: STR is
  123.       -- Prints out a string version of the array of the components 
  124.       -- that are under $STR, and their associated indices
  125.       res ::= #FSTR("{");
  126.       loop  
  127.      p ::= pair!;
  128.      res := res+",".separate!("["+ind_str(p.t1)+"]="+elt_str(p.t2));  
  129.       end;
  130.       res := res +"}";
  131.       return(res.str);
  132.    end;
  133.  
  134.    str_of_elts: STR is
  135.       -- Prints out a string version of the array of the components 
  136.       -- that are under $STR, and their associated indices
  137.       res ::= #FSTR("");
  138.       loop res:=res+",".separate!(elt_str(elt!)); end;
  139.       return(res.str);
  140.    end;
  141.  
  142.    --              ------ Basic Operations ----------------
  143.    private ind_str(i: K): STR is
  144.       typecase i
  145.       when $STR then return i.str  else return "Unprintable Index" end;
  146.    end;
  147.  
  148.    private elt_str(e: E): STR is
  149.       typecase e 
  150.       when $STR then return e.str  else return "Unprintable Element" end;
  151.    end;
  152.  
  153. end;
  154. --=============================================================================
  155. partial class MULTIMAP_INCL{K,E} is
  156.    -- Partial class for full fledged multi-maps
  157.    include RO_MULTIMAP_INCL{K,E};
  158.    
  159. end;
  160. --=============================================================================
  161.